//
//  SNSummaryInfoViewController.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/28.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNLandInformationController.h"
#import "SNSummaryInfoViewController.h"
#import "SNSummaryInfoCell.h"
static UIButton * __saveButton;
@interface SNSummaryInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
/** tableView */
@property (nonatomic,weak) UITableView * tableView;
/** saveButton */
@property(weak, nonatomic) UIButton * saveButton;
@property (nonatomic,strong) SNProductionPlanListResult * localProductionPlanListResult;
@end

@implementation SNSummaryInfoViewController
+ (UIButton *)getSaveButton{
 
    return __saveButton;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"self.productionPlanListResult.landList = %@",self.productionPlanListResult.landList);
    [self.tableView reloadData];
    
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"概要信息";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view, typically from a nib.
    self.localProductionPlanListResult = self.productionPlanListResult.copy;
    [self addBackgroundView];
    [self addTableView];
    [self addBottomButton];
    
    
}
- (void)addBottomButton
{
    dispatch_async(dispatch_get_main_queue(), ^{
        
        CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:3]];
        
        CGRect rect = [self.tableView convertRect:rectInTableView toView:self.view];
        
        NSLog(@"rect CGRect:-%@", NSStringFromCGRect(rect));
        
        UIButton * saveButton = ({
            UIButton * button = [[UIButton alloc] init];
            
            [button setTitle:@"保存" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            
            [button setBackgroundImage:[SNUtils imageWithColor:HexRGB(0x67B245)] forState:UIControlStateNormal];
            [button setBackgroundImage:[SNUtils imageWithColor:HexRGB(0xAED39D)] forState:UIControlStateDisabled];
            
            [button addTarget:self action:@selector(saveButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
            
            CGFloat y = CGRectGetMaxY(rect)+16;
            
            button.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 140)*0.5 ,y, 140, 40);
            button.layer.cornerRadius = 4;
            button.layer.masksToBounds = YES;
            [self.view addSubview:button];
            
            
            
            button;
        });
        
        self.saveButton = saveButton;
        __saveButton = saveButton;
        self.saveButton.enabled = [self checkTheShow];
    });
}
- (BOOL)checkTheShow
{
    NSInteger count = 0;
    UITableView * tableView = self.tableView;
    for(int i = 0; i < 3; i++) {
        SNSummaryInfoCell * cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:i]];
        
        if (i==2) {
            if (cell.rightTextField.text.length > 0) {
                count ++;
            }
        }else{
            if (cell.iconButton.currentTitle.length > 0) {
                count++;
            }
        }
    }
    
    if (count == 3) {
        return YES;
    }else{
        return NO;
    }
}
-(void)saveButtonClicked:(UIButton *)sender {
    NSLog(@"%s", __func__);
    
    
    if(self.localProductionPlanListResult.landList.count > 0){
        SNSummaryInfoCell * cellTemp = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]];
        
        __block NSInteger sum = 0;
        [self.localProductionPlanListResult.landList enumerateObjectsUsingBlock:^(SNProductionPlanListLandList * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            sum += obj.plantArea.integerValue;
        }];
        
        if (cellTemp.rightTextField.text.integerValue < sum) {
            [SNUtils alertViewWithViewController:self message:@"所选地块面积超过种植面积, 请重新输入!" confirmBlock:^{
                // 确定之后要做的事...
            }];
            return ;
        }
    }else{
        
    }
    
    
    
    
    for(int i = 0; i < 4; i++) {
        SNSummaryInfoCell * cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:i]];
        
        switch (i) {
            case 0:
                self.productionPlanListResult.cropTypeName = cell.iconButton.currentTitle;
                break;
            case 1:
                self.productionPlanListResult.startTime = cell.iconButton.currentTitle;
                break;
            case 2:
                self.productionPlanListResult.plantingArea = cell.rightTextField.text;
                break;
            case 3:
                //TODO: 从另一个控制器改
                // self.productionPlanListResult.landList = nil;
                self.productionPlanListResult.landList = self.localProductionPlanListResult.landList;
                break;
            default:
                break;
        }
        
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)addBackgroundView
{
//    
//    
//    
//        UIView * backgroundView = [[UIView alloc] init];
//        
//    backgroundView.backgroundColor = kNormalBackgroundColor;
//    
//      
//         [self.view addSubview:backgroundView];
    
    CALayer * layer = [CALayer layer];
    
    /** center & bounds */
    layer.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height -64);
    /** color */
    layer.backgroundColor = kNormalBackgroundColor.CGColor;
    
    [self.view.layer addSublayer:layer];
    
}
- (void)addTableView
{

    /** y坐标 */
    CGFloat y = 64;
    /** 高度 tableView */
    CGFloat height = self.view.bounds.size.height - y;
    /** 样式 tableView */
    UITableViewStyle tableViewStyle = UITableViewStyleGrouped;
    
    CGRect frame = CGRectMake(12, y, self.view.bounds.size.width-24, height);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:frame style:tableViewStyle];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = kNormalBackgroundColor;
    tableView.scrollEnabled = NO;
    [self setupInit:tableView];
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //    tableView.separatorColor = [UIColor greenColor];
    [self.view addSubview:tableView];
    
    [self addObserver];
    [self addRequest];
}
#pragma mark - <addRequest>
- (void)addRequest {
    // addRequest
}
#pragma mark - <addObserver>
- (void)addObserver {
    // addObserver
}
- (void)dealloc {
    NSLog(@"%@ %s %@",[self class], __func__ , @"im dying");
}
/** 初始化 tableView */
- (void)setupInit:(UITableView *)tableView
{
    //MARK: - 设置tableHeaderView
    //    {
    //        UIView * tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    //        tableView.tableHeaderView = tableHeaderView;
    //    }
    
    //MARK: - 注册
    {
        /** xib 注册cell */
            registerForCellFromeNib(tableView,SNSummaryInfoCell);
        /** class 注册cell */
//        registerForCellFromeClass(tableView,UITableViewCell);
        
        /** xib 注册 headerSection */
        //    registerForHeaderFooterViewFromeNib(tableView,UITableViewHeaderFooterView);
        /** class 注册 headerSection */
        //    registerForHeaderFooterViewFromeClass(tableView,UITableViewHeaderFooterView);
        
        /** xib 注册 footerSection */
        //    registerForHeaderFooterViewFromeNib(tableView,UITableViewHeaderFooterView);
        /** class 注册 footerSection */
        //    registerForHeaderFooterViewFromeClass(tableView,UITableViewHeaderFooterView);
    }
    //MARK: - 设置高度
    {
        /** header */
        tableView.sectionHeaderHeight = 10;
        // tableView.estimatedSectionHeaderHeight = 100;
        // tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
        /** cell */
        tableView.estimatedRowHeight = 100;
        tableView.rowHeight = UITableViewAutomaticDimension;
        /** footer */
        tableView.sectionFooterHeight = 0.001;
        // tableView.estimatedSectionFooterHeight = 100;
        // tableView.sectionFooterHeight = UITableViewAutomaticDimension;
    }
}

#pragma mark - <UITableViewDataSource>
/** 组数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 4;
}
/** 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
//MARK: -
//MARK: - cell
/** cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SNSummaryInfoCell * cell = DequeueForCell(SNSummaryInfoCell);
    //    SNTableViewCell * cell = [SNTableViewCell cellWithTableView:tableView];
    [cell configCellWithIndexPath:indexPath productionPlanListResult:self.localProductionPlanListResult];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.nav = self.navigationController;
    
    return cell;
}
#pragma mark - <UITableViewDelegate>
/** 自定义section header */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UITableViewHeaderFooterView * headerView = DequeueForHeaderFooterView(UITableViewHeaderFooterView);
    
    // SNHeaderView * headerView = [SNHeaderView headerViewWithTableView:tableView];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.001;
}
/** 选中一行 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __func__);
    
    switch (indexPath.section) {
        case 0://作物种类
            
            break;
        case 1://种植时间
            
            break;
        case 2://种植面积
            
            break;
        case 3://地块信息
        {
            for(int i = 0; i < 3; i++) {
                SNSummaryInfoCell * cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:i]];
                
                switch (i) {
                    case 0:
                        self.localProductionPlanListResult.cropTypeName = cell.iconButton.currentTitle;
                        break;
                    case 1:
                        self.localProductionPlanListResult.startTime = cell.iconButton.currentTitle;
                        break;
                    case 2:
                        self.localProductionPlanListResult.plantingArea = cell.rightTextField.text;
                        break;
                    default:
                        break;
                }
                
            }
            
            
            SNLandInformationController * vc = [SNLandInformationController new];
            vc.localProductionPlanListResult = self.localProductionPlanListResult;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }
    
}
///** 取消 选中 */
//- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) {
//    NSLog(@"%s", __func__);
//}
/** 可编辑 */
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}
/** 删除样式 */
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return UITableViewCellEditingStyleDelete;
//}
/** 删除当前行 */
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//
//        [self.models removeObjectAtIndex:indexPath.row];
//
//        [tableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationLeft];
//}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@ | %s",self.class,__func__);
}
@end
